package com.aaa.service.sds.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.sds.TestPlanSds;
import com.aaa.dao.sds.TestPlanSdsDao;
import com.aaa.service.sds.TestPlanSdsService;
import com.aaa.utils.MyTools;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 测试计划fpa(TestPlanSds)表服务实现类
 *
 * @author makejava
 * @since 2022-09-01 20:23:19
 */
@Service("testPlanSdsService")
public class TestPlanSdsServiceImpl implements TestPlanSdsService {
    @Resource
    private TestPlanSdsDao testPlanSdsDao;


    /**
     * @param pageSortEntity
     * @param testPlanSds
     * @return
     * @create by: 张恒
     * @description: 查询所有
     * @create time: 2022/9/1 20:59
     */
    @Override
    public Page queryTestPlanSdsPage(PageSortEntity pageSortEntity, TestPlanSds testPlanSds) {
        //分页加排序
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit(), pageSortEntity.getOrderString());
        List<Map<String, Object>> mapList = testPlanSdsDao.queryTestPlanSdsPage(testPlanSds);
        //强转为page
        Page page = (Page) mapList;
        return page;
    }

    /**
     * @param testPlanSds
     * @return
     * @create by: 张恒
     * @description: 添加
     * @create time: 2022/9/1 20:59
     */
    @InsertAnn
    @Override
    public boolean insertTestPlanSds(TestPlanSds testPlanSds) {
        while (true) {

            TestPlanSds testPlanSdsNew = new TestPlanSds();
            //此处生成六位的随机数,不足六位的数字前自动补零
            String sixRandom = MyTools.getSixRandom();
            testPlanSdsNew.setTestCode(sixRandom);
            //判断是否跟数据库已经存在的邀请码重复
            List<Map<String, Object>> mapList = testPlanSdsDao.queryAllView(testPlanSdsNew);
            if (ObjectUtil.isEmpty(mapList) || mapList.size() == 0) {
                //退出循环之前，将合法的随机数保存到实体对象中testPlanSds
                testPlanSds.setTestCode(sixRandom);
                break;
            }
        }

        return testPlanSdsDao.insertTestPlanSds(testPlanSds);
    }

    /**
     * @param testPlanSds
     * @return
     * @create by: 张恒
     * @description: 修改
     * @create time: 2022/9/1 20:59
     */
    @UpdateAnn
    @Override
    public boolean updateTestPlanSds(TestPlanSds testPlanSds) {
        return testPlanSdsDao.updateTestPlanSds(testPlanSds);
    }

    /**
     * @param id
     * @return
     * @create by: 张恒
     * @description: 删除
     * @create time: 2022/9/1 20:59
     */
    @Override
    public boolean deleteTestPlanSdsById(Integer[] id) {
        return testPlanSdsDao.deleteTestPlanSdsById(id);
    }

    /**
     * @param testPlanSds
     * @return
     * @create by: 张恒
     * @description: 唯一校验
     * @create time: 2022/9/1 20:59
     */
    @Override
    public boolean repetitionName(TestPlanSds testPlanSds) {
        Integer integer = testPlanSdsDao.repetitionName(testPlanSds);
        if (integer > 0) {
            return false;
        }
        return true;
    }
}
